package com.example.administrator.bmobbase;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class MainActivity extends AppCompatActivity {
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn_add;
    private Button btn_del;
    private Button btn_qur;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initBtn();
        /*注册*/
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               /* useBmobUser();*/
               useMyUser();

            }
        });
       /*登陆*/
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BmobUser bmobUser=new BmobUser();
                bmobUser.setUsername("王元");
                bmobUser.setPassword("12356");
                bmobUser.login(new SaveListener<BmobUser>() {
                    @Override
                    public void done(BmobUser bmobUser, BmobException e) {
                    if (e==null){
                        toast("登陆成功"+bmobUser.getUsername());
                    }
                    else {
                        toast("登陆失败" + e.getMessage());
                         }
                    }
                });

            }
        });
        /*查询用户信息*/
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyUser myUser=BmobUser.getCurrentUser(MyUser.class);
                myUser.setSex("男");
                myUser.setAge(20);
                myUser.update(new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e==null){
                            toast("更新成功");
                        }
                        else {
                            toast("更新失败" + e.getMessage());
                        }
                    }
                });
            }
        });

        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Student student=new Student();
                student.setName("李位置");
                student.setAge(15);
                student.setProfession("网络");
                student.setScore(100);
                student.save(new SaveListener<String>() {
                    @Override
                    public void done(String s, BmobException e) {
                        if (e==null){
                            toast("保存成功"+s.toString());
                        }
                        else{
                            toast("保存失败"+e.getMessage());
                        }
                    }
                });
            }
        });
        /*查询学生*/
        btn_qur.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BmobQuery<Student> studentBmobQuery=new BmobQuery<>();
                studentBmobQuery.addWhereEqualTo("profession","软件");
                studentBmobQuery.findObjects(new FindListener<Student>() {
                    @Override
                    public void done(List<Student> list, BmobException e) {
                   if(e==null){
                       StringBuffer stringBuffer=new StringBuffer();
                       for(Student student:list){
                           stringBuffer.append(student.toString());
                       }
                       toast(stringBuffer.toString());
                   }
                    }
                });
            }
        });
        /*删除*/
        btn_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BmobQuery<Student> studentBmobQuery=new BmobQuery<>();
                studentBmobQuery.addWhereEqualTo("profession","软件");
                studentBmobQuery.findObjects(new FindListener<Student>() {
                    @Override
                    public void done(List<Student> list, BmobException e) {
                        if(e==null){
                            StringBuffer stringBuffer=new StringBuffer();
                            for(Student student:list){
                                student.delete(new UpdateListener() {
                                    @Override
                                    public void done(BmobException e) {
                                        if (e==null){
                                            toast("删除成功");
                                        }
                                        else{
                                            toast("删除失败"+e.getMessage());
                                        }
                                    }
                                });
                            }
                            toast(stringBuffer.toString());
                        }
                    }
                });
            }
        });
    }

    private void useMyUser() {
        final MyUser myUser=new MyUser();
        myUser.setUsername("杨超越");
        myUser.setPassword("123456");
        myUser.setSex("男");
        myUser.setAge(18);
        myUser.signUp(new SaveListener<MyUser>() {
            @Override
            public void done(MyUser myUser1, BmobException e) {
                if (e==null){
                    toast("注册成功"+myUser1.getUsername());
                }
                else{
                    toast("注册失败"+e.getMessage());
                }
            }
        });
    }

    private void useBmobUser() {
        BmobUser bmobUser=new BmobUser();
        bmobUser.setUsername("周杰伦");
        bmobUser.setPassword("12356");
        bmobUser.signUp(new SaveListener<BmobUser>() {
            @Override
            public void done(BmobUser bmobUser, BmobException e) {
                if (e==null){
                    toast("注册成功"+bmobUser.getUsername());
                }
                else{
                    toast("注册失败"+e.getMessage());
                }
            }
        });

    }

    public void toast(String s){
            Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();
        }
    private void initBtn() {
        btn1=findViewById(R.id.btn_reg);
        btn2=findViewById(R.id.btn_log);
        btn3=findViewById(R.id.btn_info);
        btn_add=findViewById(R.id.btn_add);
        btn_del=findViewById(R.id.btn_del);
        btn_qur=findViewById(R.id.btn_qur);
    }
}
